//
//  XMTitleAlterView.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/5/5.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMTitleAlterView.h"

@interface XMTitleAlterView ()

{
    titleAlterViewSureBlock _sureBlock;
    titleAlterViewCancelBlock _cancelBlock;
}
//背景view
@property(nonatomic,strong) UIControl *coverControl;
//内容view
@property (nonatomic,strong) UIView * contentView;
//标题
@property (nonatomic,copy) NSString * title;
//标题
@property (nonatomic,strong) UILabel * titleLabel;
//确定
@property (nonatomic,strong) UIButton * sureBtn;
//取消
@property (nonatomic,strong) UIButton * cancelBtn;
@end

@implementation XMTitleAlterView

//选择展示回调
+ (void)showTypeMaterialListWithTitle:(NSString *)title sureBlock:(titleAlterViewSureBlock)sureBlock cancelBlock:(titleAlterViewCancelBlock)cancelBlock{
    
    XMTitleAlterView * alterView = [[XMTitleAlterView alloc]initWithTitle:title sureBlock:sureBlock cancelBlock:cancelBlock];
    [alterView show];
    
}

-(instancetype)initWithTitle:(NSString *)title sureBlock:(titleAlterViewSureBlock)sureBlock cancelBlock:(titleAlterViewCancelBlock)cancelBlock{
    
    if (self = [super init]) {
        self.title = title;
        _sureBlock = sureBlock;
        _cancelBlock = cancelBlock;
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    self.backgroundColor = kClearColor;
    
    //背景control
    [self.coverControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    //内容
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@300);
        make.height.equalTo(@(180));
    }];
}


#pragma mark 动画

- (void)show{
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(keyWindow);
    }];
    
    self.contentView.alpha = 0;
    self.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        weakSelf.contentView.alpha = 1;
    }];
}

- (void)hide
{
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.alpha = 0;
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {
        [self.coverControl removeAllSubviews];
        [self.coverControl removeFromSuperview];
        [self.contentView removeAllSubviews];
        [self.contentView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

#pragma mark ----action----



//确定
-(void)sureAction{
    if (_sureBlock) {
        _sureBlock();
    }
    [self hide];
}
//取消
-(void)cancelAction{
    
    if (_cancelBlock) {
        _cancelBlock();
    }
    [self hide];
}

#pragma mark ----getting&setting----

//背景颜色control
-(UIControl *)coverControl{
    if (!_coverControl) {
        _coverControl = [[UIControl alloc]init];
        _coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self addSubview:_coverControl];
        
    }
    return _coverControl;
}

//内容
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = kWhiteColor;
        _contentView.layer.cornerRadius = 4.0f;
        _contentView.clipsToBounds = YES;
        

        //确定
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(_contentView);
            make.left.equalTo(_contentView.mas_centerX);
            make.height.equalTo(@45);
        }];
        
        //取消
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.equalTo(_contentView);
            make.right.equalTo(_contentView.mas_centerX);
            make.height.equalTo(@45);
        }];
        
        //间隔
        UIView * marginView = [[UIView alloc]init];
        marginView.backgroundColor = HEX_RGB(0xeaeaea);
        [_contentView addSubview:marginView];
        
        [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_contentView);
            make.centerY.equalTo(self.sureBtn);
            make.height.equalTo(@30);
            make.width.equalTo(@1);
        }];
        
        //标题
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_contentView).offset(15);
            make.right.equalTo(_contentView).offset(-15);
            make.centerY.equalTo(_contentView);
            make.centerX.equalTo(_contentView);
        }];
        
        UILabel * alterTitleLabel = [[UILabel alloc]init];
        alterTitleLabel.text = @"提示";
        alterTitleLabel.textAlignment = NSTextAlignmentCenter;
        [_contentView addSubview:alterTitleLabel];
        
        [alterTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.centerX.equalTo(self.titleLabel);
            
            make.bottom.equalTo(self.titleLabel.mas_top).offset(-20);
            
        }];
        
        [self addSubview:_contentView];
    }
    return _contentView;
    
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = kBoldFont(16);
        _titleLabel.textColor = kTextBlackColor;
        _titleLabel.text = self.title;
        _titleLabel.numberOfLines = 0;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
    
}

//确定
-(UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = kFont(14);
        [_sureBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
        [_sureBtn setBackgroundColor:kBgColor];
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_sureBtn];
    }
    return _sureBtn;
}

//取消
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kFont(14);
        [_cancelBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
        [_cancelBtn setBackgroundColor:kBgColor];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

@end
